package jbl.dao.impl;

import java.io.InputStream;
import java.io.Serializable;
import java.sql.Blob;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;

import jbl.dao.BaseDaoI;

import org.hibernate.Hibernate;
import org.hibernate.LobHelper;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

@Repository("baseDao")
public class BaseDaoImpl<T> implements BaseDaoI<T> {
	private SessionFactory sessionFactory;

	public SessionFactory getSessionFactory() {
		return sessionFactory;
	} 

	@Autowired
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}
	/**
	 * 得到currentSession
	 * @return
	 */
	private Session getCurrentSession(){
		
		return this.sessionFactory.getCurrentSession();
		
	}
	
	@Override
	public Serializable save(T o) {

		return this.sessionFactory.getCurrentSession().save(o);
	}

	@Override
	public T get(String hql) {
		Query q= this.sessionFactory.getCurrentSession().createQuery(hql);
		List<T> l=q.list();
		if(l!=null&&l.size()>0){
			
			
			return l.get(0);
		}
		return null;
	}
	@Override
	public T get(Class<T> c, Serializable id) {
		return (T)sessionFactory.getCurrentSession().get(c, id);
	}

	@Override
	public T get(String hql, Object[] params) {
		
		Query q= this.sessionFactory.getCurrentSession().createQuery(hql);
		if(params!=null&&params.length>0){
			
			for(int i=0;i<params.length;i++){
				
				q.setParameter(i, params[i]);
			}
			
		}
		
		List<T> l=q.list();
		if(l!=null&&l.size()>0){
			
			
			return l.get(0);
		}
		return null;
	}
	/**
	 * 推荐使用这种方式
	 */
	@Override
	public T get(String hql, Map<String, Object> params) {
		Query q= this.sessionFactory.getCurrentSession().createQuery(hql);
		if(params!=null&&!params.isEmpty()){
			for(String key:params.keySet()){
				q.setParameter(key, params.get(key));
			}
		}
		List<T> l=q.list();
		if(l!=null&&l.size()>0){
			
			
			return l.get(0);
		}
		return null;
	}

	@Override
	public void delete(T o) {
		this.getCurrentSession().delete(o);
	}

	@Override
	public void update(T o) {
		this.getCurrentSession().update(o);
	}

	@Override
	public void saveOrUpdate(T o) {
		this.getCurrentSession().saveOrUpdate(o);
		
	}

	@Override
	public List<T> find(String hql) {
		
		Query q=this.getCurrentSession().createQuery(hql);
		return q.list();
	}

	@Override
	public List<T> find(String hql, Map<String, Object> params) {
		Query q=this.getCurrentSession().createQuery(hql);
		if(params!=null&&!params.isEmpty()){
			for(String key:params.keySet()){
				Object obj = params.get(key);  
                //这里考虑传入的参数是什么类型，不同类型使用的方法不同  
                if(obj instanceof Collection<?>){  
                    q.setParameterList(key, (Collection<?>)obj);  
                }else if(obj instanceof Object[]){  
                    q.setParameterList(key, (Object[])obj);  
                }else{  
                    q.setParameter(key, obj);  
                }  
			}
			}
			return q.list();
		
	}


	@Override
	public List<T> find(String hql, Map<String, Object> params, int page, int rows) {
			
			Query q=this.getCurrentSession().createQuery(hql);
			if(params!=null&&!params.isEmpty()){
				for(String key:params.keySet()){
				q.setParameter(key, params.get(key));
				}
				}
				return q.setFirstResult((page-1)*rows).setMaxResults(rows).list();
			
	}

	@Override
	public List<T> find(String hql, int page, int rows) {
		Query q=this.getCurrentSession().createQuery(hql);
		return q.setFirstResult((page-1)*rows).setMaxResults(rows).list();
	}
	@Override
	public String countString(String hql) {
		Query q=this.getCurrentSession().createQuery(hql);
		return q.uniqueResult().toString();
	}
	@Override
	public String countString(String hql, Map<String, Object> params) {
		Query q=this.getCurrentSession().createQuery(hql);
		if(params!=null&&!params.isEmpty()){
			for(String key:params.keySet()){
			q.setParameter(key, params.get(key));
			}
			}
		if(q.uniqueResult()==null){
			
			return null;
		}
		return q.uniqueResult().toString();
	}
	@Override
	public Long count(String hql) {
		
		Query q=this.getCurrentSession().createQuery(hql);
		return (Long)q.uniqueResult();
	}
	@Override
	public Integer lastInsertID(String hql) {
		Query q=this.getCurrentSession().createQuery(hql);
		return (Integer)q.uniqueResult();
	}
 
	@Override
	public Long count(String hql, Map<String, Object> params) {
		Query q=this.getCurrentSession().createQuery(hql);
		if(params!=null&&!params.isEmpty()){
			for(String key:params.keySet()){
			q.setParameter(key, params.get(key));
			}
			}
			return (Long)q.uniqueResult();
	}
	@Override
	public Integer executeHql(String hql) {
		return this.getCurrentSession().createQuery(hql).executeUpdate();
	}
	@Override
	public Integer executeHql(String hql, Object[] param) {
		Query q = this.getCurrentSession().createQuery(hql);
		if (param != null && param.length > 0) {
			for (int i = 0; i < param.length; i++) {
				q.setParameter(i, param[i]);
			}
		}
		return q.executeUpdate();
	}
	@Override
	public Integer executeHql(String hql, List<Object> param) {
		Query q = this.getCurrentSession().createQuery(hql);
		if (param != null && param.size() > 0) {
			for (int i = 0; i < param.size(); i++) {
				q.setParameter(i, param.get(i));
			}
		}
		return q.executeUpdate();
	}

	@Override
	public Blob getBlob(InputStream in, Long length) {
		return this.getCurrentSession().getLobHelper().createBlob(in, length);
	}

	@Override
	public T load(Class<T> c, Serializable id) {
		return (T)sessionFactory.getCurrentSession().load(c, id);
	}

	@Override
	public float sum(String hql, Map<String, Object> params) {
		return 0;
	}

	@Override
	public Double sum(String hql) {
		Query q= this.sessionFactory.getCurrentSession().createQuery(hql);
		List<Double> l=q.list();
		if(l!=null&&l.size()>0){
			
			
			return (Double) l.get(0);
		}
		return null;
	}

	@Override
	public void mer(T o) {
		this.sessionFactory.getCurrentSession().merge(o);
	}


	
}
